package cn.ivicar.oss.resource;

import cn.com.carsmart.exception.CarsmartException;
import cn.com.carsmart.ws.util.CacheUtil;
import cn.ivicar.oss.controller.CustomerController;
import cn.ivicar.oss.controller.SimInfoController;
import cn.ivicar.oss.controller.VehicleController;
import cn.ivicar.oss.entity.ResponseEntity;
import com.google.inject.Inject;
import com.google.inject.servlet.RequestScoped;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

/**
 * Created by Administrator on 2017/5/21.
 */
@Path("customer")
@RequestScoped
public class CustomerResource {
    private static transient final Logger LOGGER = LoggerFactory.getLogger(CustomerResource.class);

    @Inject
    private CustomerController customerController;
    @Inject
    private VehicleController vehicleController;
    @Inject
    private SimInfoController simInfoController;

    @Path("data")
    @GET
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response getCustomerData(@QueryParam("mobile") String mobile) throws CarsmartException {

        JSONObject customer = this.customerController.getCustomerByMobile(mobile);
        JSONArray jsonArray = new JSONArray();
        JSONObject result = new JSONObject();
        if (customer != null) {
            jsonArray.add(customer);
        }
        result.put("data", jsonArray);
        return CacheUtil.noCache(Response.ok(result)).build();
    }

    @Path("datas")
    @GET
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response getCustomers() throws CarsmartException {

        JSONArray customerList = this.customerController.getCustomerList();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data", customerList);

        return CacheUtil.noCache(Response.ok(jsonObject)).build();
    }

    @POST
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    @Consumes(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response updateCustomer(JSONObject jsonObject) throws CarsmartException {

        ResponseEntity responseEntity = this.customerController.updateCustomer(jsonObject);

        return CacheUtil.noCache(Response.ok(responseEntity)).build();
    }

    @DELETE
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response deleteCustomer(@QueryParam("id") String id) throws CarsmartException {
        ResponseEntity responseEntity = this.customerController.deleteCustomer(id);
        return CacheUtil.noCache(Response.ok(responseEntity)).build();
    }

    @Path("vehicles")
    @GET
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response getVehicles(@QueryParam("cid") String cid) throws CarsmartException {

        JSONArray vehicles = this.customerController.getVehicles(cid);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data", vehicles);
        jsonObject.put("type","customerToVehicle");

        return CacheUtil.noCache(Response.ok(jsonObject)).build();
    }


    @Path("cvs")
    @GET
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response getCVSRelation(@QueryParam("cid") String cid) throws CarsmartException {

        JSONObject content = new JSONObject();
        if (StringUtils.isNotBlank(cid)) {

            JSONObject customer = this.customerController.getCustomerById(Long.valueOf(cid));
            //暂时转为数组
            content.put("vehicle", new String[] {});
            content.put("customer", new String[] {});
            content.put("simInfo", new String[] {});
            if (customer != null) {
                String[] c = new String[7];
                c[0] = customer.optString("id");
                c[1] = customer.optString("email");
                c[2] = customer.optString("mobile");
                c[3] = customer.optString("nickName");
                c[4] = customer.optString("sex");
                c[5] = customer.optString("birthday");
                c[6] = customer.optString("created");
                content.put("customer", c);
            }

            Long vid = this.customerController.getVehicleIdByCId(Long.valueOf(cid));
            if (vid != null) {
                JSONObject vehicle = this.vehicleController.getVehicleById(vid);
                //暂转为数组
                if (vehicle != null) {
                    String[] v = new String[7];
                    v[0] = vehicle.optString("vid");
                    v[1] = vehicle.optString("name");
                    v[2] = vehicle.optString("serial");
                    v[3] = vehicle.optString("plate");
                    v[4] = vehicle.optString("vin");
                    v[5] = vehicle.optString("ein");
                    v[6] = vehicle.optString("created");
                    content.put("vehicle", v);
                }

                String iccid = this.vehicleController.getSimIccidByVid(vid);
                if (iccid != null) {
                    JSONObject simInfo = this.simInfoController.getSimInfoByIccid(iccid);
                    //暂转为数组
                    if (simInfo != null) {
                        String[] sim = new String[8];
                        sim[0] = simInfo.optString("iccid");
                        sim[1] = simInfo.optString("userCode");
                        sim[2] = simInfo.optString("operator");
                        sim[3] = simInfo.optString("carStatus");
                        sim[4] = simInfo.optString("smsStatus");
                        sim[5] = simInfo.optString("effectTime");
                        sim[6] = simInfo.optString("orgId");
                        sim[7] = simInfo.optString("created");
                        content.put("simInfo", sim);
                    }
                }
            }
        }

        return CacheUtil.noCache(Response.ok(content)).build();
    }

    @Path("vehicle")
    @DELETE
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response deleteVehicleRel(@QueryParam("cid") String cid,@QueryParam("vid") String vid) throws CarsmartException {
        ResponseEntity responseEntity = this.customerController.deleteVehicle(cid,vid);
        return CacheUtil.noCache(Response.ok(responseEntity)).build();
    }

    @Path("vehicle")
    @POST
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response addVehicleRel(JSONObject jsonObject) throws CarsmartException {
        ResponseEntity responseEntity = this.customerController.addVehicleRel(jsonObject);
        return CacheUtil.noCache(Response.ok(responseEntity)).build();
    }
}
